<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="4" :xs="24">
        <div class="app-container-tree-border">
          <el-tree :data="syTypeList" :props="defaultProps" :expand-on-click-node="false" ref="tree" node-key="id"
            highlight-current @node-click="handleNodeClick">
            <div slot-scope="{node, data}" class="custom-tree-node" :title="node.label" v-text="node.label"></div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <div class="app-container-table-border">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="0px" class="top-left">
            <!-- <el-form-item label prop="reportName">
              <el-input v-model="queryParams.reportName" placeholder="请输入试验名称" clearable
                @keyup.enter.native="handleQuery" style="width: 180px;" />
            </el-form-item> -->
            <el-form-item label prop="reportDate">
              <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                range-separator="～" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item label prop="ypCode">
              <el-input v-model="queryParams.ypCode" placeholder="请输入样品编号" clearable @keyup.enter.native="handleQuery"
                style="width: 180px;" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <el-row :gutter="10" class="mb5 top-right-btn small-line-height">
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                v-hasPermi="['syrw:bsDataReport:export']">导出</el-button>
            </el-col>
            <el-popover v-if="Array.isArray(titleList)" placement="bottom-start" width="300" trigger="click"
              v-model="showPopover" popper-class="report-popper-campus">
              <div class="cd-p8">
                <el-tree :data="titleList" :props="defaultProps" :expand-on-click-node="false" highlight-current>
                  <div slot-scope="{node, data}" class="custom-tree-node" :title="node.label">
                    {{ node.label }}
                    <el-tag :type="data.showTitle ? '' : 'info'" size="mini" class="text-mini pointer"
                      @click="() => { data.showTitle = !data.showTitle }">
                      {{ data.showTitle ? '显示' : '未显示' }}
                    </el-tag>
                  </div>
                </el-tree>
              </div>
              <el-button slot="reference" size="mini" class="cd-mr16">列筛选 <i class="el-icon-arrow-down"></i></el-button>
            </el-popover>
          </el-row>
          <template v-if="hasTitle">
            <el-table v-if="Array.isArray(titleList)" class="report-table" border stripe v-loading="loading"
              :data="dataList">
              <el-table-column label="序号" header-align="center" align="center" type="index" width="55px" />
              <el-table-column v-if="item.showTitle" v-for="(item, index) in titleList" :key="'first' + index"
                :label="item.label" align="center" :min-width="(item.width || '120')" show-overflow-tooltip>
                <template slot-scope="scope">{{ getTableValue(scope.row, item.value) }}</template>
                <el-table-column v-if="sItem && sItem.showTitle" v-for="(sItem, sIndex) in item.children"
                  :key="'second' + sIndex" :label="sItem.label" align="center" :min-width="(sItem.width || '120')"
                  show-overflow-tooltip>
                  <template slot-scope="scope">
                    {{ getTableValue(scope.row, sItem.value) }}
                  </template>
                  <el-table-column v-if="tItem.showTitle" v-for="(tItem, tIndex) in sItem.children"
                    :key="'third' + tIndex" :label="tItem.label" align="center" :min-width="(tItem.width || '70')"
                    show-overflow-tooltip>
                    <template slot-scope="scope">{{ getTableValue(scope.row, tItem.value) }}
                    </template>
                  </el-table-column>
                </el-table-column>
              </el-table-column>
            </el-table>
            <div v-else class="placeholder">
              <el-empty description="正在开发中..." class="cd-mt32"></el-empty>
            </div>
          </template>
          <div class="placeholder" v-loading="loading" v-else></div>
          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { syTpyeTree } from "@/api/base/syType";
import { getTaiZhangList } from "@/api/statistics";
import { getReportTitle } from "@/utils/report";

export default {
  components: {
  },
  name: "Sytz",
  data () {
    return {
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 试验类型列表
      syTypeList: [],
      // 遮罩层
      loading: false,
      // 日期范围
      dateRange: [],
      // 显示搜索条件
      showSearch: true,
      showPopover: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        syType: undefined,
        deptId: undefined,
        reportName: undefined,
        reportDate: undefined,
        ypCode: undefined,
      },
      total: 0,
      dataList: [],
      titleList: getReportTitle(),
      hasTitle: true,
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["deptId"])
  },
  watch: {
    deptId (newValue, oldValue) {
      this.queryParams.deptId = newValue;
      if (this.queryParams.syType) {
        this.getList();
      }
    }
  },
  created () {
    this.queryParams.deptId = this.$store.state.dept.deptId;
    this.getSyTypeTreeList();
  },
  methods: {
    /** 查询试验类型树 */
    getSyTypeTreeList () {
      let param = {
        level: 2,
        innerFlag: "1"
      };
      syTpyeTree(param).then((response) => {
        this.syTypeList = response.data;
      });
    },
    handleNodeClick (data) {
      this.queryParams.syType = undefined;
      if (data.level == 2) {
        this.queryParams.syType = data.id;
        this.handleQuery();
      }
    },
    /** 查询数据报告列表 */
    getList () {
      if (!this.queryParams.deptId) {
        this.$message.error("请先选择一个试验室");
        return;
      }
      if (!this.queryParams.syType) {
        this.$message.warning("请选择一个合法的试验类型");
        return;
      }
      this.loading = true;
      this.hasTitle = false;
      this.showPopover = false;
      getTaiZhangList(this.addDateRange(this.queryParams, this.dateRange, 'ReportDate')).then((response) => {
        let titleList = getReportTitle(this.queryParams.syType);
        if (Array.isArray(titleList)) {
          titleList = JSON.parse(JSON.stringify(titleList));
        }
        this.titleList = titleList;
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.hasTitle = true;
        this.$forceUpdate();
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getTableValue (row, type) {
      // 分隔符
      let arr = type.split(".");
      let value = null;
      arr.forEach(item => {
        if (value) {
          value = value[item];
        } else {
          value = row[item];
        }
      });
      return value;
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "tjfx/exportTz",
        {
          ...this.queryParams
        },
        `试验台账_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>

<style lang="scss" scoped>
.table-add {
  text-align: center;
  line-height: 23px;
  padding: 4px;
  border: 1px solid #dfe6ec;
  border-top: 0px solid #dfe6ec;
}

.placeholder {
  height: 450px;
  width: 100%;
}

::v-deep .report-table {
  .el-table__header-wrapper th {
    height: 22px !important;
  }
}
</style>
